package xiao_jin.fantastic_journey.server.event;

import club.someoneice.cookie.event.SubEvent;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.passive.SnowGolemEntity;
import net.minecraft.util.math.MathHelper;
import xiao_jin.fantastic_journey.core.handler.SnowballHitEvent;

public final class ServerEvent {
    @SubEvent
    public static void onHit(SnowballHitEvent event) {
        if (!(event.getHitResult() instanceof LivingEntity livingEntity)) {
            return;
        }
        float yaw = -event.getSnowball().getYaw();
        if (event.getOwner() instanceof SnowGolemEntity) {
            livingEntity.takeKnockback(0, MathHelper.sin(yaw * MathHelper.PI / 180), -MathHelper.cos(yaw * MathHelper.PI / 180));
        }
        StatusEffectInstance effect = livingEntity.getStatusEffect(StatusEffects.SLOWNESS);
        int amplifier = effect == null ? -1 : effect.getAmplifier();
        amplifier = (amplifier >= 2) ? 2 : (amplifier + 1);
        livingEntity.addStatusEffect(new StatusEffectInstance(StatusEffects.SLOWNESS, (amplifier + 1) * 20, amplifier));
    }
}
